<?php

function document_template_list_page()
{
    @$output = drupal_render(drupal_get_form('document_template_list_form'));
    return $output;
}

function document_template_list_form($form, &$form_state)
{
//    $search_img = _is_mobile()?"":"<img src='".base_path()."sites/all/themes/tweme/images/search.png'>";
//    $res_img = _is_mobile()?"":"<img src='".base_path()."sites/all/themes/tweme/images/result.png'>";
//    $form['border_wrapper']['#markup'] =$search_img.'<div class="border_wrapper-search">';
//    $form['template_list_beg']['#markup'] = '<div class="template_search">';
    $form['fuzzy_title'] = array(
        '#type' => 'textfield',
        '#default_value' => '',
        '#attributes' => array('placeholder' => '表单名称'),
        '#validated' => TRUE,
    );
//    $form['template_list_close']['#markup'] ='</div>';
//    $form['help']['#markup'] = _is_mobile()?"":'<div id="help"><a href="'.base_path().'" data-toggle="modal" target = "_blank">
//                                        <img src="'.base_path().'sites/all/themes/tweme/images/help.png"/>
//                                 </a></div>';"</div><div class='table-div'>$res_img<div class='table-border-div'>".."</div></div>"
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function data_list_view_page($form, &$form_state)
{
    $output = '';
    $header = array(
        array('data' => '选择', 'class' => ['essential no']),
        array('data' => '表单ID', 'class' => ['essential form_identifier']),
        array('data' => '表单名称', 'class' => ['essential title']),
        array('data' => '文档数据库的链接', 'class' => ['essential break collection']),
        array('data' => '操作', 'class' => ['essential more']),
    );
    $rows = array();

//    if($form_state['triggering_element']['#value'] == '查询') {

        $query = db_select('document_template', 'MT')->extend('PagerDefault')->extend('TableSort');
        $query->fields('MT');
        $query->condition('MT.title', '%' . db_like($form_state['values']['fuzzy_title']) . '%', 'LIKE');
        $query->limit(variable_get("list_page_item_number", 5));
        $result = $query->orderByHeader($header)->execute();


        $n = 1;
        foreach ($result as $row) {
            $rows[] = array(
                array('data' => '<input type="checkbox" class="listViewTableCheck" value ="' . $row->document_template_id . '"/>' . $n++, 'class' => ['essential no']),
                array('data' => $row->form_identifier, 'class' => ['essential form_identifier']),
                array('data' => $row->title, 'class' => ['essential title']),
                array('data' => $row->collection, 'class' => ['essential break collection']),
                array('data' => '<a href="'.base_path().'document_template/edit/' . $row->document_template_id . '" data-toggle="modal"><img width="20px" height="20px" src="'.base_path().'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'essential more'),
            );
        }
//    }

    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
    if (sizeof($rows) > 0) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }
    $output .= theme('pager');
    return $output;
}

function document_template_delete()
{
    ajax_table_delete('document_template', 'document_template_id');
}

